package yygh.order.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import yygh.hospital.enums.OrderStatusEnum;
import yygh.hospital.model.order.OrderInfo;
import yygh.hospital.vo.order.OrderCountQueryVo;
import yygh.hospital.vo.order.OrderQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yygh.common.result.Result;
import yygh.common.utils.AuthContextHolder;
import yygh.order.Service.OrderService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {


    @Autowired
    OrderService orderService;

    //订单接口1：生成预约挂号订单
    @ApiOperation("生成预约挂号订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result savaOrders(@PathVariable String scheduleId,
                             @PathVariable Long patientId){
        Long orderId= orderService.saveOrder(scheduleId,patientId);
        return  Result.ok(orderId);
    }


    //订单接口2：根据订单编号查询订单详情
    //根据订单id查询订单详情
    @ApiOperation("根据订单id查询订单详情")
    @GetMapping("auth/getOrders/{orderId}")
    public  Result getOrders(@PathVariable String orderId){
        OrderInfo orderInfo=orderService.getOrder(orderId);
        return  Result.ok(orderInfo);
    }

    //订单接口3：实现订单列表功能（条件分页查询）
    @ApiOperation("获取订单列表")
    @GetMapping("{page}/{limit}")
    public Result  list(@PathVariable Long pageNum,
                        @PathVariable Long pageSize,
                        OrderQueryVo orderQueryVo,
                        HttpServletRequest request){
        //只查询当前用户下的所有订单
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));

        //首先将分页信息进行分装
        Page<OrderInfo> page = new Page<>(pageNum, pageSize);

        //进行条件分页查询。
        IPage<OrderInfo> infoIPage=orderService.selectPage(page,orderQueryVo);
        return  Result.ok(infoIPage);
    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }


    /**
     * 订单接口4：实现预约取消操作
     */
    @ApiOperation("实现预约取消操作")
    @GetMapping("auth/cancelOrder/{orderId}")
    public  Result cancelOrder(String orderId){
      Boolean isRefund = orderService.cancelOrder(orderId);
        return  Result.ok(isRefund);
    }


    /**
     * 订单接口5：获取预约订单的统计数据
     */
    @ApiOperation("获取预约订单的统计数据")
    @PostMapping("inner/getCountMap")
    public  Result getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo){
        Map<String,Object> map=orderService.getCountMap(orderCountQueryVo);
        return  Result.ok(map);
    }

}
